package plus.decl;

import java.util.List;

import plus.Gripe;
import plus.Identifier;
import plus.Symbol;
import plus.Type;
import plus.TypeSym;

public class PT_Named extends TypeSpec {

	private Identifier ident;
	private List<TypeSpec> pSpecs;

	public PT_Named(Identifier ident, List<TypeSpec> pSpecs) {
		this.ident = ident;
		this.pSpecs = pSpecs;
	}

	@Override
	public Type interpret() {
		//System.out.println(ident.toString());
		return constructor().constructWith(actuals());
	}
	private TypeSym constructor() {
		Symbol<TypeSym> sts = ident.asType();
		if (null == sts) throw new Gripe("No such type: "+ident);
		// TODO if we had defining expressions in symbols we could try recursion
		if (null == sts.definition) throw new Gripe("Type not yet defined: "+ident);
		return sts.definition;
	}
	private Type[] actuals() {
		if (pSpecs == null) return null;
		Type[] actuals = new Type[pSpecs.size()];
		int i=0;
		for (TypeSpec sp: pSpecs) {
			actuals[i++] = sp.interpret();
		}
		return actuals;
	}
}
